package com.beijiteshop.shop.ui.coupon.vm

import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.onBizEmpty
import com.beijiteshop.libcommon.network.ext.onBizError
import com.beijiteshop.libcommon.network.ext.onBizOK
import com.beijiteshop.libcommon.network.ext.serverData
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.shop.model.api.response.Coupon
import com.beijiteshop.shop.ui.coupon.repo.CouponRepo

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/17 17:44
 * @Description
 */
class CouponVM(private val repo: CouponRepo) : BaseViewModel() {

    val liveCoupons = MutableLiveData<MutableList<Coupon>>()

    fun getCoupons(page: Int,status: String) = launch {
        repo.getCoupons(page, status)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    if (data.isNullOrEmpty()){
                        liveEmpty.postValue(null)
                    }else{
                        liveCoupons.postValue(data)
                    }
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveEmpty.postValue(it)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }
}